package com.lyn.mvp.view.activity

import android.app.ProgressDialog
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.observe
import androidx.viewbinding.ViewBinding
import com.lyn.mvp.R
import com.lyn.mvp.model.Result
import com.lyn.scan.mvvm.presenter.Presenter

/**
 * @author Lyn
 * @date 2022/7/13
 * @desc
 */
abstract class BaseActivity<V : ViewBinding> : AppCompatActivity() {

    lateinit var binding: V
    private var progress: ProgressDialog?=null
    lateinit var presenter: Presenter
    val liveData = MutableLiveData<Result>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        liveData.observe(this) {
            onChanged(it)
        }
        presenter = Presenter(liveData)
        binding = getViewBinding()
        setContentView(binding.root)
        create(savedInstanceState)
    }

    abstract fun getViewBinding(): V;

    abstract fun create(savedInstanceState: Bundle?)

    fun loading() {
        progress?.dismiss()
        progress = null
        progress = ProgressDialog(this)
        progress!!.setTitle(getString(R.string.prompt))
        progress!!.setMessage(getString(R.string.plasewait))
        progress!!.setCanceledOnTouchOutside(false)
        progress!!.show()
    }

    fun hideLoading() {
        progress?.dismiss()
        progress = null
    }


    abstract fun onChanged(result: Result)

    fun toast(msg: String) {
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show()
    }

}